//
//  ContactCell.m
//  Guazi
//
//  Created by koolearn on 11-5-8.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ContactCell.h"
#import "Status.h"
#import "ImageStore.h"
#import "FriendDetailViewController.h"
#import "GuaziAppDelegate.h"


@implementation ContactCell


@synthesize user;
@synthesize searchText;
@synthesize isSearching;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        cellView = [[ContactCellView alloc] initWithFrame:CGRectZero];
		[self.contentView addSubview:cellView];
        
        imageButton = [[UIButton alloc] initWithFrame:CGRectZero];
		[imageButton addTarget:self action:@selector(showProfile) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:imageButton];
    }
    return self;
}


- (void)updateImage:(UIImage*)image
{
    //self.imageView.image = image;
    //[self layoutSubviews];
    imageButton.imageView.image = image;
    [imageButton setNeedsDisplay];
}


- (void) setSearchText:(NSString *)text
{
    searchText = [text retain];
    cellView.searchText = text;
}


- (void) setIsSearching:(BOOL)b
{
    isSearching = b;
    cellView.isSearching = b;
}


- (void) update:(User*)auser frame:(CGRect)rect
{
	self.user = auser;
	cellView.user = auser;
	
    cellView.backgroundColor = [UIColor whiteColor];
	cellView.frame = rect;

	if ( auser.profileImageUrl_s != nil ) {
		//self.imageView.image = [self getProfileImage:auser.profileImageUrl_s isLarge:NO];
        //imageButton.imageView.image = [self getProfileImage:auser.profileImageUrl_s isLarge:NO];
        
        UIImage* image = [self getProfileImage:auser.profileImageUrl_s isLarge:NO];
        [imageButton setImage:image forState:UIControlStateNormal];
    }
	else {
		//self.imageView.image = [ImageStore defaultProfileImage:NO];
        //imageButton.imageView.image = [ImageStore defaultProfileImage:NO];
        UIImage* image = [ImageStore defaultProfileImage:NO];
        [imageButton setImage:image forState:UIControlStateNormal];
    }
    
    imageButton.frame = CGRectMake(5, 5, 50, 50);

	[cellView setNeedsDisplay];
	[cellView setNeedsLayout];
	[self setNeedsDisplay];
	[self setNeedsLayout];
}


- (void)layoutSubviews
{
    //self.imageView.frame = CGRectMake(2, 2, 50, 50);
    imageButton.frame = CGRectMake(5, 5, 50, 50);
    [super layoutSubviews];
    //self.backgroundColor = self.contentView.backgroundColor;
    //cellView.backgroundColor = self.contentView.backgroundColor;
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


+ (CGFloat) getCellHeight:(User*)aUser isSearching:(BOOL)searching
{
    CGFloat height = [ContactCellView getViewHeight:aUser isSearching:searching];
    if ( height < 60 ) height = 60;
    return height;
    /*
	CGFloat height = 40;
	if ( aUser.phone != nil && [aUser.phone length] > 0 ) {
		height += 20;
	}
	
	Status* status = aUser.status;
	if ( status != nil ) {
		height += status.textBounds.size.height + 20;
	}
	
	if ( height < 60 ) height = 60;
	return height;*/
}


- (void) showProfile
{
    FriendDetailViewController* controller = [[FriendDetailViewController alloc] initWithUser:user];
	[[[GuaziAppDelegate getAppDelegate] currentNavigationController] pushViewController:controller animated:YES];
    [controller release];
}


- (void)dealloc {
	[user release];
	[cellView release];
    [imageButton release];
    [super dealloc];
}


@end
